package com.iweb.dao.impl;

import com.iweb.dao.EmpDao;
import com.iweb.pojo.Emp;

import javax.sql.DataSource;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @author: jack
 * @date: 2022-08-04 0004
 * @ApiNOTE:
 */
public class EmpDapImpl implements EmpDao {

  private DataSource dataSource;

  public void setDataSource(DataSource dataSource) {
    this.dataSource = dataSource;
  }

  @Override
  public Emp selectEmpAccount(String account) {
    Emp emp = null;
    try {
      Connection conn = dataSource.getConnection();

      String sql = "select * from tb_emp where account = ?";
      PreparedStatement pst = conn.prepareStatement(sql);
      pst.setString(1, account);

      ResultSet rs = pst.executeQuery();
      while (rs.next()) {
        emp = new Emp(
                rs.getInt(1),
                rs.getString(2),
                rs.getString(3),
                rs.getString(4),
                rs.getString(5),
                rs.getDate(6),
                rs.getInt(7),
                rs.getString(8),
                rs.getInt(9)
        );
      }

    } catch (SQLException e) {
      e.printStackTrace();
    }
    return emp;
  }

  @Override
  public Integer selectMerchantIdByEmpId(Integer empId) {
    Connection conn = null;
    try {
      conn = dataSource.getConnection();
      PreparedStatement pst = conn.prepareStatement("select mid from tb_emp where eid = ?");
      pst.setInt(1, empId);
      ResultSet rs = pst.executeQuery();
      while (rs.next()) {
        return rs.getInt(1);
      }
    } catch (Exception e) {
      e.printStackTrace();
    } finally {
      try {
        conn.close();
      } catch (SQLException e) {
        e.printStackTrace();
      }
    }
    return null;
  }
}
